package com.qixin.modules.eaLoan.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qixin.modules.ea.vo.ListPageVo;
import com.qixin.modules.eaBasic.vo.EaTaskVo;
import com.qixin.modules.eaBasic.vo.IdVo;
import com.qixin.modules.eaLoan.entity.EaLoanbill;
import com.qixin.modules.eaLoan.mapper.EaLoanbillMapper;
import com.qixin.modules.eaLoan.service.IEaLoanbillService;
import com.qixin.modules.eaLoan.service.IEaLoanbillServiceCopy;
import com.qixin.modules.eaLoan.vo.EaLoanbillPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.EmptyUtil;
import org.jeecg.common.util.FillRuleUtil;
import org.jeecg.modules.activity.service.ActivityCommonService;
import org.jeecg.modules.nc.entity.NcPersonnel;
import org.jeecg.modules.nc.service.INcPersonnelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 借款单
 * @Author: qixin-boot
 * @Date:   2021-07-20
 * @Version: V1.0
 */
@Api(tags="app借款单")
@RestController
@RequestMapping("/app/api/eaLoanbill")
@Slf4j
public class ApiEaLoanbillController {
    @Autowired
    private IEaLoanbillService eaLoanbillService;
    @Autowired
    private INcPersonnelService ncPersonnelService;
    // 工作流公共执行类
    @Autowired
    private ActivityCommonService activityCommonService;

//    /**
//     * 查询借款单信息列表
//     *
//     * @param listPageVo
//     * @return Result
//     */
//    @ApiOperation(value="借款单信息列表-查询", notes="借款单信息列表-查询")
//    @PostMapping(value = "/list")
//    public Result<?> list(@RequestBody ListPageVo listPageVo) {
//        Map<String,Object> resutlList = eaLoanbillService.list(listPageVo);
//        return Result.OK(resutlList);
//    }

    /**
    * @Description
    * @author  wujinbiao
    * @date 2022/2/17
    * @param[ pageNo, pageSize, keyword, req]
    * @return org.jeecg.common.api.vo.Result<?>
    */

    @ApiOperation(value = "app借款单-关联报销单查询列表", notes = "app借款单-关联报销单查询列表")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   @RequestParam(name="keyword", required = false) String keyword,
                                   @RequestParam(name="org", required = false) String org,
                                   HttpServletRequest req) {
        QueryWrapper<EaLoanbill> param = new QueryWrapper<>();
        //update-begin-by wjb 2022-02-17 16:43 for 添加查询条件 列表排序，创建人
        param.orderByDesc("create_time");
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // 当前登录用户创建，并且审批通过，未被报销
        param.eq("create_by", loginUser.getId()).eq("state",2);
        if(StringUtils.isNotBlank(keyword)){
            param.and(
                p->p.like("bill_no",keyword).or().like("explain_str",keyword)
            );
        }
        if(StringUtils.isNotBlank(org)){
            param.eq("company_code_id",org);
        }
        param.and(
                wrapper -> wrapper.isNull("ea_repaymentbill_id").or().eq("ea_repaymentbill_id","")
        );
        //update-end-by wjb for 添加查询条件
        Page<EaLoanbill> page = new Page<EaLoanbill>(pageNo, pageSize);
        IPage<EaLoanbill> pageList = eaLoanbillService.page(page, param);
        List<EaLoanbill> records = pageList.getRecords();
        for (EaLoanbill eaLoanbill1 : records) {
            NcPersonnel ncPersonnel = ncPersonnelService.queryById(eaLoanbill1.getPersonId());
            if (ncPersonnel != null) {
                eaLoanbill1.setPersonIdName(ncPersonnel.getName());
            }
        }
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param eaLoanbill
     * @return
     */
    @AutoLog(value = "借款单-添加")
    @ApiOperation(value = "借款单-添加", notes = "借款单-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody EaLoanbill eaLoanbill) {
        // update-begin-Autor: wjb date: 2022-02-09 10:47 for: 生成单据编号
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("prefix","JK"); // 编码前缀
        Object result = FillRuleUtil.executeRule("common_code", jsonObj);
        eaLoanbill.setBillNo(result.toString());
        // update-end-Autor: wjb date: 2022-02-09 10:47 for: 生成单据编号
        eaLoanbill.setState(0);
        eaLoanbillService.save(eaLoanbill);
        // update-begin-author wjb 2022-02-16 15:50 for返回收款人中文
        if (StringUtils.isNotBlank(eaLoanbill.getReceiver())){
            NcPersonnel ncPersonnel = ncPersonnelService.queryById(eaLoanbill.getReceiver());
            if(ncPersonnel != null){
                eaLoanbill.setReceiverName(ncPersonnel.getName());
            }
        }
        // update-end-author wjb 2022-02-16 15:50 for返回收款人中文
        return Result.OK(eaLoanbill);
    }

//    /**
//     * 编辑借款单信息
//     *
//     * @param eaLoanbillPage
//     * @return
//     */
//    @AutoLog(value = "借款单信息-编辑")
//    @ApiOperation(value="借款单信息-编辑", notes="借款单信息-编辑")
//    @PostMapping(value = "/edit")
//    public Result<?> edit(@RequestBody EaLoanbillPage eaLoanbillPage) {
//        EaLoanbill eaLoanbill = new EaLoanbill();
//        BeanUtils.copyProperties(eaLoanbillPage, eaLoanbill);
//        EaLoanbill eaLoanbillEntity = eaLoanbillService.getById(eaLoanbill.getId());
//        if(eaLoanbillEntity==null) {
//            return Result.error("未找到对应数据");
//        }
//
//        //判断单据是否已经被提交
//        EaLoanbill eaLoanbill1 = eaLoanbillService.getById(eaLoanbillPage.getId());
//        String status = EmptyUtil.checkNull(eaLoanbill1.getState());   //当状态为1审批中,2已审批时,提示单据状态已变更,请刷新
//        if(status.equals("1") || status.equals("2")){
//            return Result.error("单据状态已变更,请刷新");
//        }
//
//        eaLoanbill.setSaveType("add");    //保存
//        Map<String,Object> returnMap = eaLoanbillService.updateMain(eaLoanbill);
//
//        if(returnMap.get("code").equals("-1")){
//            return Result.error(EmptyUtil.checkNull(returnMap.get("message")));
//        }else{
//            returnMap.remove("code");
//            returnMap.remove("message");
//            return Result.OK(returnMap);
//        }
//    }

    /**
     * 编辑
     *
     * @param eaLoanbill
     * @return
     */
    @AutoLog(value = "借款单-编辑")
    @ApiOperation(value = "借款单-编辑", notes = "借款单-编辑")
    @PostMapping(value = "/edit")
    public Result<?> edit(@RequestBody EaLoanbill eaLoanbill) {
        eaLoanbillService.updateById(eaLoanbill);
        // update-begin-author wjb 2022-02-16 15:50 for返回收款人中文
        if (StringUtils.isNotBlank(eaLoanbill.getReceiver())){
            NcPersonnel ncPersonnel = ncPersonnelService.queryById(eaLoanbill.getReceiver());
            if(ncPersonnel != null){
                eaLoanbill.setReceiverName(ncPersonnel.getName());
            }
        }
        // update-end-author wjb 2022-02-16 15:50 for返回收款人中文
        return Result.OK(eaLoanbill);
    }


//    /**
//     * 查询借款单详情
//     *
//     * @param idVo
//     * @return
//     */
//    @ApiOperation(value="借款单详情-查询", notes="借款单详情-查询")
//    @PostMapping(value = "/selectById")
//    public Result<?> selectById(@RequestBody IdVo idVo) {
//        String id = idVo.getId();
//        EaLoanbill eaLoanbill = eaLoanbillMapper.selectById(id);
//        if(eaLoanbill==null) {
//            return Result.error("未找到对应数据");
//        }
//        Map<String, Object> resutlList = eaLoanbillService.selectById(id);
//        return Result.OK(resutlList);
//    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "借款单-通过id查询", notes = "借款单-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        EaLoanbill eaLoanbill = eaLoanbillService.getById(id);
        if (eaLoanbill == null) {
            return Result.error("未找到对应数据");
        }
        // 返回借款人中文
        if(StringUtils.isNotBlank(eaLoanbill.getPersonId())){
            NcPersonnel ncPersonnel = ncPersonnelService.queryById(eaLoanbill.getPersonId());
            if (ncPersonnel != null){
                eaLoanbill.setPersonIdName(ncPersonnel.getName());
            }
        }
        // update-begin-author wjb 2022-02-16 15:50 for返回收款人中文
        if (StringUtils.isNotBlank(eaLoanbill.getReceiver())){
            NcPersonnel ncPersonnel = ncPersonnelService.queryById(eaLoanbill.getReceiver());
            if(ncPersonnel != null){
                eaLoanbill.setReceiverName(ncPersonnel.getName());
            }
        }
        // update-end-author wjb 2022-02-16 15:50 for返回收款人中文
        return Result.OK(eaLoanbill);
    }

//    /**
//     * 通过id删除借款单
//     *
//     * @param idVo
//     * @return
//     */
//    @AutoLog(value = "借款单信息-通过id删除")
//    @ApiOperation(value="借款单信息-通过id删除", notes="借款单信息-通过id删除")
//    @PostMapping(value = "/delete")
//    public Result<?> delete(@RequestBody IdVo idVo) {
//        String id = idVo.getId();
//        Map<String,Object> returnMap = eaLoanbillService.delMain(id);
//        if(returnMap.get("code").equals("0")){
//            return Result.OK("删除成功!");
//        }else{
//            String message = returnMap.get("message") != null ? returnMap.get("message").toString() : "";
//            return Result.error(message);
//        }
//    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "借款单-通过id删除")
    @ApiOperation(value = "借款单-通过id删除", notes = "借款单-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        QueryWrapper<EaLoanbill> params = new QueryWrapper<>();
        params.eq("id",id);
        EaLoanbill eaLoanbill = eaLoanbillService.getOne(params);
        if(eaLoanbill != null && eaLoanbill.getState() > 0){
            return Result.error("该单据已发起流程,不可删除!");
        }
        eaLoanbillService.removeById(id);
        return Result.OK("删除成功!");
    }

//    /**
//     * 借款单审批/退回
//     *
//     * @param eaTaskVo
//     * @return
//     */
//    @AutoLog(value = "借款单审批/退回")
//    @ApiOperation(value="借款单审批/退回", notes="借款单审批/退回")
//    @PostMapping(value = "/audit")
//    public Result<?> edit(@RequestBody EaTaskVo eaTaskVo) {
//        Map<String,Object> returnMap = eaLoanbillService.audit(eaTaskVo);
//        return Result.OK(returnMap);
//    }

}
